//
//  VisitorLoginView.swift
//  我的微博
//
//  Created by yz on 15/9/7.
//  Copyright © 2015年 yz. All rights reserved.
//

import UIKit

protocol VisitorLoginViewDelegate:NSObjectProtocol{
    func visitorLoginViewWillLogin()
    func visitorLoginViewWillRegister()
}

class VisitorLoginView: UIView {

    /// 定义代理 - 一定要用weak
    weak var delegate:VisitorLoginViewDelegate?
    // 按钮监听方法
    func clickLogin(){
        delegate?.visitorLoginViewWillLogin()
    }
    func clickRegister(){
        delegate?.visitorLoginViewWillRegister()
    }
    
    // MARK: --- 设置视图信息
    func setupViewInfo(isHome:Bool,imageName:String,message:String){
        messeageLable.text = message
        iconView.image = UIImage(named: imageName)
        homeIconView.hidden = !isHome
        isHome ? startAnimation() : sendSubviewToBack(maskIconView)
    }
    // MARK:初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    private func startAnimation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        animation.removedOnCompletion = false
        iconView.layer.addAnimation(animation, forKey: nil)
    }

    required init?(coder aDecoder: NSCoder) {
    //        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }
    
    // 设置界面
    private func setupUI(){
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messeageLable)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 1.设置iconView的自动布局
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
           addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -50))
        
        // 2.设置homeIconView的自动布局
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        // 3.设置messeageLable的自动布局
        messeageLable.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messeageLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
         addConstraint(NSLayoutConstraint(item: messeageLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messeageLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 230))
        
        // 4.注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messeageLable, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messeageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        // 5.登陆按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messeageLable, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messeageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        // 6.遮罩视图
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subView]-0-|", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: ["subView" : maskIconView]))
         addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subView]-(-35)-[registerBTN]", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: ["subView" : maskIconView,"registerBTN":registerButton]))
      
        // 7.设置背景颜色
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1)
        
    }
    
    // MARK:懒加载控件
    // 图标
    private lazy var iconView:UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
    }()
    
    // 遮罩
    private lazy var maskIconView:UIImageView = {
       let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return iv
    }()
    // 小房子
    private lazy var homeIconView:UIImageView = {
       let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
    }()
    // 消息文字
    private lazy var messeageLable:UILabel = {
        let lable = UILabel()
        lable.text = "关注一些人,回到这里看看有什么惊喜"
        lable.font = UIFont.systemFontOfSize(14)
        lable.textAlignment = NSTextAlignment.Center
        lable.numberOfLines = 0
        lable.textColor = UIColor.darkGrayColor()
        lable.sizeToFit()
        return lable
    }()
    
    // 注册按钮
    private lazy var registerButton:UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "clickRegister", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    // 登录按钮
    private lazy var loginButton:UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "clickLogin", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
}
